unit uSelectEmp;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxLookAndFeels,
  cxLookAndFeelPainters, Vcl.Menus, dxSkinsCore, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, cxControls, cxStyles, dxSkinscxPCPainter, cxCustomData,
  cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator, Data.DB, cxDBData,
  cxTextEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, Vcl.StdCtrls,
  cxButtons, Vcl.ExtCtrls, Datasnap.DBClient, ClientRowSet, cxContainer,
  cxGroupBox, cxCheckBox ;

type
  TfrmSelectEmp = class(TForm)
    crsEmp: TClientRowSet;
    crsEmpEMPLOYEE_ID: TStringField;
    crsEmpEMP_NO: TStringField;
    crsEmpEMP_NAME: TStringField;
    crsEmpEMP_MOBILE: TStringField;
    crsEmpSYS_ACCOUNT: TStringField;
    crsEmpVERSION: TIntegerField;
    crsEmpGROUP_CODE: TStringField;
    crsEmpNODE_NO: TStringField;
    crsEmpNODE_NAME: TStringField;
    dsEmp: TDataSource;
    GrdEmp: TcxGrid;
    tvEmp: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGroupBox1: TcxGroupBox;
    cxButton1: TcxButton;
    Label2: TLabel;
    edtEmpNo: TcxTextEdit;
    cxButton2: TcxButton;
    crsEmpSELECTED: TBooleanField;
    tvEmpSELECTED: TcxGridDBColumn;
    procedure cxButton1Click(Sender: TObject);
    procedure tvEmpDblClick(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    isAll:Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSelectEmp: TfrmSelectEmp;
  procedure SelectEmpNoShow( var empNo, empName,empTel,Node,NodeName,empId: string;sNode:string='';sDuty:string='');
  procedure SelectEmpIDShow(var empId:string);
  procedure SelectEmpBatchShow(var idList,noList,nameList:TStringList);
implementation

{$R *.dfm}

uses ucommFunction;

procedure SelectEmpBatchShow(var idList,noList,nameList:TStringList);
var
  i:Integer;
begin
  Application.CreateForm(TfrmSelectEmp, frmSelectEmp);
  with frmSelectEmp do
  begin
    try
      isAll:=False;
      cxButton2Click(nil);
      tvEmpSELECTED.Visible:=True;
      if ShowModal=mrOK then
      begin
        for I := 1 to crsEmp.RecordCount do
        begin
          crsEmp.RecNo:=I;
          if crsEmp.FieldByName('SELECTED').AsBoolean then
          begin
            idList.Add(crsEmp.FieldByName('EMPLOYEE_ID').AsString);
            noList.Add(crsEmp.FieldByName('emp_no').AsString);
            nameList.Add(crsEmp.FieldByName('emp_name').AsString);
          end;
        end;
      end;
    finally
      FreeAndNil(frmSelectEmp);
    end;
  end;
end;


procedure SelectEmpIDShow(var empId:string);
var
  sql:string;
begin
  empId := '';
  Application.CreateForm(TfrmSelectEmp, frmSelectEmp);
  with frmSelectEmp do
  try
    isAll:=True;
    cxButton2Click(nil);
    if ShowModal = mrOK then
    begin
      empId := crsEmp.FieldByName('employee_id').AsString;
    end;
  finally
    FreeAndNil(frmSelectEmp);
  end;
end;

procedure SelectEmpNoShow( var empNo, empName,empTel,Node,NodeName,empId: string;sNode:string;sDuty:string);
var
  sql:string;
  sNodeList: string;
begin
  empNo := '';
  empName := '';
  Application.CreateForm(TfrmSelectEmp, frmSelectEmp);
  with frmSelectEmp do
  try
    isAll:=True;
    sql:= 'SELECT e.employee_id,e.emp_no,e.emp_name,e.emp_mobile,e.sys_account,e.group_code,e.node_no,' +
      ' n.node_name,e.version FROM bas_employee e LEFT JOIN sys_group_node n ON n.node_no=e.node_no ' +
      ' WHERE e.group_code=' + QuotedStr(userInfo.groupCode) + ' and n.group_code='+QuotedStr(userInfo.groupCode)+
      ' AND e.disabled=0 AND e.state=0';
    if not userInfo.isCenter then
      sql:=sql+' and e.Node_NO in '+userInfo.NodeStr;
    if sNode <> '' then
    begin
      sNodeList := fGetNodeList(sNode);
      sql := sql + ' and e.node_no in ' + sNodeList;
    end;
    if sDuty<>'' then
      sql := sql + ' and e.duty in ' + QuotedStr(sDuty);
    crsEmp.Close;
    crsEmp.CommandText:=sql;
    crsEmp.Open;
    if ShowModal = mrOK then
    begin
      empNo := crsEmp.FieldByName('emp_no').AsString;
      empName := crsEmp.FieldByName('emp_name').AsString;
      empTel:= crsEmp.FieldByName('emp_mobile').AsString;
      Node:=crsEmp.FieldByName('node_no').AsString;
      NodeName :=crsEmp.FieldByName('Node_name').AsString;
      empId:=crsEmp.FieldByName('employee_id').AsString;
    end;
  finally
    FreeAndNil(frmSelectEmp);
  end;
end;

procedure TfrmSelectEmp.cxButton1Click(Sender: TObject);
begin
  ModalResult := mrOk;
end;

procedure TfrmSelectEmp.cxButton2Click(Sender: TObject);
var
  tmpStr: string;
begin
  tmpStr := 'SELECT ' + GetClientRowSetColumns(crsEmp) + ' FROM bas_employee WHERE group_code=' + QuotedStr(userInfo.groupCode) +
    ' AND disabled=0 AND state=0 and '+fGetDataScope('node_no');
//  if not userInfo.isCenter then
//    tmpStr:=tmpStr+' and Node_NO in '+userInfo.NodeStr;
  if Trim(edtEmpNo.Text) <> '' then
    tmpStr := tmpStr + ' AND (emp_no LIKE ' + QuotedStr('%' + edtEmpNo.Text + '%') +
      ' OR emp_name LIKE ' + QuotedStr('%' + edtEmpNo.Text + '%') + ')';
  if not isAll then
    tmpStr:=tmpStr+' and emp_no in (select user_no from sys_user where group_code='+QuotedStr(userInfo.groupCode)+')';
  OpenDataSet(tmpStr, crsEmp);
end;

procedure TfrmSelectEmp.FormCreate(Sender: TObject);
begin
  pSetCaption(Self);
end;

procedure TfrmSelectEmp.tvEmpDblClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

end.
